<?php
/**
 * Created by PhpStorm.
 * User: dev
 * Date: 5/9/14
 * Time: 3:18 PM
 */
class Categories extends Eloquent {

    public static $table = 'categories';
    public static $key = 'id';



    function __construct(){
        parent::__construct();

    }

    public static function parent($pid=0){
        return DB::table('categories')->where('parent_id', '=', $pid);
    }

    public static function child($pid = 0 ){
        $result = DB::table('categories')->where('parent_id', '=', $pid)->take(50);
        if($result){
            return $result;
        }
    }

    public static function total(){
        return Categories::count();
    }

    public static function counter($id){
        if(!empty($id)){
            $ctr = DB::table('terms')
                ->where('term','=','category')
                ->where('value','=',$id)
                ->count();
            return $ctr;
        }else{
            return 0;
        }
    }


    public static function html_link($pid=null, $limit=3){
        if(!is_null($pid)){
            $cat = Categories::parent($pid)->take($limit)->get();
            $link = '';
            foreach ($cat as $cat){
                //$link .= $cat->name.', ';
                $url = Location::get().'/'.$cat->slug;
                $link.="<a class='' href='$url' title='$cat->name'> ". Str::limit($cat->name,12)." </a>, ";
            }

            //$link =  Str::limit(strip_tags($link), 50);

            $length = strlen(trim($link));
            $link = substr_replace($link, '', $length - 1, $length);
            return $link;

        }
    }
}